﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Koonsoft.HYCRM.DAL;
using Koonsoft.HYCRM.Model;

namespace Koonsoft.HYCRM.BLL
{
    public class LoginConditionBLL
    {
        public DataTable GetEmplyeeLoginCondition(DateTime dateStart, DateTime dateEnd, int branchId)
        {
            DataTable dt = new DataTable();


            dt.Columns.Add("用户姓名", typeof(string));
            dt.Columns.Add("登录名", typeof(string));
            dt.Columns.Add("上午正常", typeof(string));
            dt.Columns.Add("上午迟到", typeof(string));
            dt.Columns.Add("上午未签到", typeof(string));
            dt.Columns.Add("下午正常", typeof(string));
            dt.Columns.Add("下午迟到", typeof(string));
            dt.Columns.Add("下午未签到", typeof(string));


            List<LoginConditionEntity> LoginConditions = new LoginConditionDAL().GetEmplyeeContractByEmplyeeId(
                dateStart, dateEnd, branchId);

            foreach (var VARIABLE in LoginConditions.GroupBy(fun => fun.LoginName))
            {
                int[] i = new int[6];
                for (DateTime date = dateStart; date <= dateEnd; date = date.AddDays(1))
                {
                    if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                    {
                        continue;
                    }
                    var list = VARIABLE.Where(fun => fun.LoginTime.ToString("yyyy-MM-dd") == date.ToString("yyyy-MM-dd"));

                    if (list.Count(fun => fun.LoginTime.Hour == 7 ||(fun.LoginTime.Hour == 8 && fun.LoginTime.Minute <=30)) > 0)
                    {
                        i[0]++;
                    }
                    else if (list.Count(fun => (fun.LoginTime.Hour == 8 && fun.LoginTime.Minute >= 30) || fun.LoginTime.Hour == 9) > 0)
                    {
                        i[1]++;
                    }
                    else
                    {
                        i[2]++;
                    }

                    if (list.Count(fun => fun.LoginTime.Hour == 13 && fun.LoginTime.Minute <= 30) > 0)
                    {
                        i[3]++;
                    }
                    else if (list.Count(fun => (fun.LoginTime.Hour == 13 && fun.LoginTime.Minute > 30) || fun.LoginTime.Hour == 14 || fun.LoginTime.Hour == 15) > 0)
                    {
                        i[4]++;
                    }
                    else
                    {
                        i[5]++;
                    }
                }

                DataRow dr = dt.NewRow();
                dr[0] = VARIABLE.First().Name;
                dr[1] = VARIABLE.Key;
                dr[2] = i[0];

                if (i[1] > 0)
                {
                    dr[3] = string.Format("<a style=\"color:#0000ff\">{0}</a>", i[1]);
                }
                else
                {
                    dr[3] = i[1];
                }

                if (i[2] > 0)
                {
                    dr[4] = string.Format("<a style=\"color:red\">{0}</a>", i[2]);
                }
                else
                {
                    dr[4] = i[2];
                }

                dr[5] = i[3];

                if (i[4] > 0)
                {
                    dr[6] = string.Format("<a style=\"color:#0000ff\">{0}</a>", i[4]);
                }
                else
                {
                    dr[6] = i[4];
                }

                if (i[5] > 0)
                {
                    dr[7] = string.Format("<a style=\"color:red\">{0}</a>", i[5]);
                }
                else
                {
                    dr[7] = i[5];
                }

                dt.Rows.Add(dr);

            }

            return dt;
        }


        public DataTable GetEmplyeeLoginCondition(DateTime dateStart, DateTime dateEnd, string loginName, int branchId=-1)
        {
            DataTable dt = new DataTable();


            dt.Columns.Add("用户姓名", typeof(string));
            dt.Columns.Add("登录名", typeof(string));
            dt.Columns.Add("日期", typeof(string));
            dt.Columns.Add("星期", typeof(string));
            dt.Columns.Add("上午", typeof(string));
            dt.Columns.Add("下午", typeof(string));


            List<LoginConditionEntity> LoginConditions = new LoginConditionDAL().GetEmplyeeContractByEmplyeeId(
                dateStart, dateEnd, branchId).Where(fun => fun.LoginName.ToLower() == loginName.ToLower()).ToList();

            string name = "";
            if (LoginConditions.Count() > 0)
            {
                name = LoginConditions.First().Name;
            }

            for (DateTime date = dateStart; date <= dateEnd; date = date.AddDays(1))
            {

                var VARIABLE = LoginConditions.Where(fun => fun.LoginTime.ToString("yyyy-MM-dd") == date.ToString("yyyy-MM-dd"));

                DataRow dr = dt.NewRow();

                dr[0] = name;
                dr[1] = loginName;
                dr[2] = date.ToString("yyyy年MM月dd日");
                dr[3] = date.DayOfWeek;

                if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                {

                }
                else
                {
                    if (VARIABLE.Count(fun => fun.LoginTime.Hour == 7 || (fun.LoginTime.Hour == 8 && fun.LoginTime.Minute <= 30)) > 0)
                    {
                        dr[4] = "正常";
                    }
                    else if (VARIABLE.Count(fun => (fun.LoginTime.Hour == 8 && fun.LoginTime.Minute >= 30) || fun.LoginTime.Hour == 9 ) > 0)
                    {
                        string loginTime =
                                VARIABLE.Where(fun => (fun.LoginTime.Hour == 8 && fun.LoginTime.Minute >= 30) || fun.LoginTime.Hour == 9).OrderBy(
                                    fun => fun.LoginTime).First().LoginTime.ToString("HH:mm:ss");
                        dr[4] = "<a style=\"color:#0000ff\">迟到（" + Math.Floor((Convert.ToDateTime(loginTime) - Convert.ToDateTime("08:30:00")).TotalMinutes) + "分钟," + loginTime + "）</a>";
                    }
                    else
                    {
                        dr[4] = "<a style=\"color:red\">未签到</a>";
                    }


                    if (VARIABLE.Count(fun => fun.LoginTime.Hour == 13 && fun.LoginTime.Minute <= 30) > 0)
                    {
                        dr[5] = "正常";
                    }
                    else if (VARIABLE.Count(fun => (fun.LoginTime.Hour == 13 && fun.LoginTime.Minute > 30) || fun.LoginTime.Hour == 14 || fun.LoginTime.Hour == 15) > 0)
                    {
                        string loginTime =
                               VARIABLE.Where(fun => (fun.LoginTime.Hour == 13 && fun.LoginTime.Minute > 30) || fun.LoginTime.Hour == 14 || fun.LoginTime.Hour == 15).OrderBy(
                                   fun => fun.LoginTime).First().LoginTime.ToString("HH:mm:ss");
                        dr[5] = "<a style=\"color:#0000ff\">迟到（" + Math.Floor((Convert.ToDateTime(loginTime) - Convert.ToDateTime("13:30:00")).TotalMinutes) + "分钟," + loginTime + "）</a>";
                  
                    }
                    else
                    {
                        dr[5] = "<a style=\"color:red\">未签到</a>";
                    }
                }

                dt.Rows.Add(dr);
            }

            return dt;
        }

        public DataTable GetEmplyeeLoginCondition(DateTime date, int branchId)
        {
            DataTable dt = new DataTable();


            dt.Columns.Add("用户姓名", typeof(string));
            dt.Columns.Add("登录名", typeof(string));
            dt.Columns.Add("星期", typeof(string));
            dt.Columns.Add("上午", typeof(string));
            dt.Columns.Add("下午", typeof(string));

            List<LoginConditionEntity> LoginConditions = new LoginConditionDAL().GetEmplyeeContractByEmplyeeId(
                date, date, branchId);

            foreach (var VARIABLE in LoginConditions.GroupBy(fun => fun.LoginName))
            {
                DataRow dr = dt.NewRow();
                dr[0] = VARIABLE.First().Name;
                dr[1] = VARIABLE.Key;
                dr[2] = date.DayOfWeek;

                if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                {

                }
                else
                {
                    if (VARIABLE.Count(fun => fun.LoginTime.Hour == 7 || (fun.LoginTime.Hour == 8 && fun.LoginTime.Minute <= 30)) > 0)
                    {
                        dr[3] = "正常";
                    }
                    else if (VARIABLE.Count(fun => (fun.LoginTime.Hour == 8 && fun.LoginTime.Minute >= 30) || fun.LoginTime.Hour == 9 ) > 0)
                    {
                        string loginTime =
                            VARIABLE.Where(fun => (fun.LoginTime.Hour == 8 && fun.LoginTime.Minute >= 30) || fun.LoginTime.Hour == 9).OrderBy(
                                fun => fun.LoginTime).First().LoginTime.ToString("HH:mm:ss");
                        dr[3] = "<a style=\"color:#0000ff\">迟到（" + Math.Floor((Convert.ToDateTime(loginTime) - Convert.ToDateTime("08:30:00")).TotalMinutes) + "分钟," + loginTime + "）</a>";
                    }
                    else
                    {
                        dr[3] = "<a style=\"color:red\">未签到</a>";
                    }

                    if (VARIABLE.Count(fun => fun.LoginTime.Hour == 13 && fun.LoginTime.Minute <= 30) > 0)
                    {
                        dr[4] = "正常";
                    }
                    else if (VARIABLE.Count(fun => (fun.LoginTime.Hour == 13 && fun.LoginTime.Minute > 30) || fun.LoginTime.Hour == 14 || fun.LoginTime.Hour == 15) > 0)
                    {
                        string loginTime =
                            VARIABLE.Where(fun => (fun.LoginTime.Hour == 13 && fun.LoginTime.Minute > 30) || fun.LoginTime.Hour == 14 || fun.LoginTime.Hour == 15).OrderBy(
                                fun => fun.LoginTime).First().LoginTime.ToString("HH:mm:ss");
                        dr[4] = "<a style=\"color:#0000ff\">迟到（" + Math.Floor((Convert.ToDateTime(loginTime) - Convert.ToDateTime("13:30:00")).TotalMinutes) + "分钟," + loginTime + "）</a>";
                    }
                    else
                    {
                        dr[4] = "<a style=\"color:red\">未签到</a>";
                    }
                }
                dt.Rows.Add(dr);
            }


            return dt;
        }


    }
}
